package com.dragon.mmochat.model.chart;

import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

public class MmochatChart {
	private DefaultCategoryDataset dataSet = new DefaultCategoryDataset();

	/**
	 * 添加数据
	 * 
	 * @param value
	 *            :数据
	 * @param rowKey
	 *            :曲线类型
	 * @param columnKey
	 *            :x轴标注
	 */
	public void addData(Number value, Comparable<?> rowKey,
			Comparable<?> columnKey) {
		dataSet.addValue(value, rowKey, columnKey);
	}

	/**
	 * 生成图表
	 * 
	 * @param is3D
	 *            ：是否三维
	 * @param chartTitle
	 *            ：图表标题
	 * @param xAxisName
	 *            ：X轴标题
	 * @param yAxisName
	 *            ：Y轴标题
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public JFreeChart createChart(boolean is3D, String chartTitle,
			String xAxisName, String yAxisName) {
		JFreeChart chart = null;
		if (is3D) {
			chart = ChartFactory.createLineChart3D(chartTitle, xAxisName,
					yAxisName, dataSet, PlotOrientation.VERTICAL, true, true,
					false);
		} else {
			chart = ChartFactory.createLineChart(chartTitle, xAxisName,
					yAxisName, dataSet, PlotOrientation.VERTICAL, true, true,
					false);
		}
		chart.setBackgroundPaint(new Color(240, 240, 240));
		CategoryPlot plot = chart.getCategoryPlot();
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		renderer
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 显示折点数据
		renderer.setItemLabelsVisible(true);

		return chart;
	}

	// String filename = ServletUtilities.saveChartAsPNG(createChart, 800, 500,
	// null,
	// request.getSession());

	// String graphURL = request.getContextPath() + "/DisplayChart?filename=" +
	// filename; %>

	// <img src="<%= graphURL %>" width=800 height=500 border=0
	// usemap="#<%= filename %>" alt="abc">
}
